<template>
  <div>
    <!--    添加按钮-->
    <div class="add" v-if="state1" v-show="state">
      <el-button
        size="mini"
        type="primary"
        plain
        @click="add">添加医院</el-button>
    </div>
    <!--    添加操作的弹框-->
    <div v-if="!state1" class="add1">
      <el-form ref="form" :model="form" label-width="80px" size="mini"  style="width: 400px">
        <el-form-item label="医院名称">
          <el-input v-model="form.name" ></el-input>
        </el-form-item>
        <el-form-item label="活动地址">
          <el-input v-model="form.location" ></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" @click="addHospital" style="width: 100px;height: 50px">立即添加</el-button>
          <el-button @click="quxiao1" style="width: 100px;height: 50px">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-show="state" v-if="state1">
      <el-table
        :data="tableData"
        style="width: 60%">
        <el-table-column
          align="center"
          label="医院名称"
          width="200">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="医院地址"
          width="300">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.location }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              style="width: 50px;height: 50px"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              style="width: 50px;height: 50px"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    弹出框-->
    <div v-show="!state" class="add1">
      <el-form ref="sizeForm" :model="sizeForm" label-width="80px" size="mini" style="width: 400px">
        <el-form-item label="医院名称">
          <el-input v-model="sizeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="医院地址">
          <el-input v-model="sizeForm.location"></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" @click="hospitalUpdate" style="width: 100px;height: 50px"> 立即修改</el-button>
          <el-button @click="quxiao" style="width: 100px;height: 50px">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name:'HospitalModule',
  data() {
    return {
      state:true,
      state1:true,
      hospitalID:'',
      sizeForm: {
        name: '',
        location:''
      },
      //添加里的属性
      form: {
        name: '',
        location:''
      },
      tableData: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    async getData(){
      await this.$store.dispatch('getHospital')
      this.showHospital()
    },
    showHospital(){
      this.tableData.splice(0,this.tableData.length)
      this.$store.state.backstage.hospital.forEach((item)=>{
        const {id,name,location} = item
        let hospitalArr = {id:id,name:name,location:location}
        this.tableData.push(hospitalArr)
      })
    },
    //编辑医院
    handleEdit(index, row) {
      this.state = false
      this.hospitalID = row.id
      //清空输入框
      this.sizeForm = {name: '',location: ''}
    },
    async hospitalUpdate(){
      let {name,location} = this.sizeForm;
      let {hospitalID} = this;
      (name && location) && await this.$store.dispatch('hospitalUpdate',{id:hospitalID,name,location})
      if (this.$store.state.backstage.updateHospital.code === 1){
        this.$message({
          message: this.$store.state.backstage.updateHospital.data,
          type: 'success'
        });
        this.getData()
        this.quxiao()
      }else {
        this.$message.error(this.$store.state.backstage.updateHospital.msg);
      }
    },
    //删除医院
    async handleDelete(index, row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let id = row.id
        await this.$store.dispatch('deleteHospital',{id:id})
        await this.getData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    quxiao(){
      this.state = true
    },
    quxiao1(){
      this.state1 = true
    },
    add(){
      this.state1 = false
      //清空输入框
      this.form = {name: '',location: ''}
    },
    async addHospital() {
      let {name,location} = this.form;
      (name && location) && await this.$store.dispatch('addHospital',{name,location})
      if (this.$store.state.backstage.addHospitalInfo.code === 1){
        this.$message({
          message: this.$store.state.backstage.addHospitalInfo.data,
          type: 'success'
        });
        this.quxiao1()
        this.getData()
      }else {
        this.$message.error(this.$store.state.backstage.addHospitalInfo.msg);
      }
    }
  }
}
</script>
<style scoped>
.add{
  position:relative;
  float: right;
  top: 10px;
  right: 500px;
  /*left: 780px;*/
  width: 100px;
  height: 50px;
  z-index: 1000;
}
/*添加弹出框的属性*/
.add1{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
</style>
